﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using WebCore.Models;

namespace WebCore.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        [AllowAnonymous]
        [HttpGet("login")]
        public IActionResult Login(string returnUrl = null)
        {
            TempData["returnUrl"] = returnUrl;
            return View();
        }

        [AllowAnonymous]
        [HttpPost("login")]
        public async Task<JsonResult> Login(string usercode, string password, string returnUrl = null)
        {
            using (DataContext dbContext = new DataContext())
            {
                var user = dbContext.emp.Where(t => t.user_name == usercode && t.password == password).FirstOrDefault();
                if (user == null)
                    return Json(new { success = false, message = "用户名或密码错误" });

                var role = dbContext.role.FirstOrDefault(t=>t.id == user.role_id);
                //用户标识
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                identity.AddClaim(new Claim(ClaimTypes.Sid, usercode));
                identity.AddClaim(new Claim(ClaimTypes.Name, user.real_name));
                identity.AddClaim(new Claim(ClaimTypes.Role, role.role_name));
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

                return Json(new { success = true, message = "登录成功" });
            }
        }


        [HttpGet("logout")]
        public async Task<IActionResult> Logout()
        {
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return RedirectToAction("Index", "Home");
        }


        [AllowAnonymous]
        [HttpGet("denied")]
        public IActionResult Denied()
        {
            return View();
        }

        public IActionResult ChangePwd()
        {
            return View();
        }

        [HttpPost("changepwd")]
        public async Task<JsonResult> ChangePwd(string OldPassword, string Password, string ConfirmPassword)
        {
            try
            {
                using (DataContext dbContext = new DataContext())
                {
                    var user = dbContext.emp.FirstOrDefault(t => t.user_name == User.Identity.Name);
                    if (user.password != OldPassword)
                        throw new Exception("旧密码不正确");

                    if (Password != ConfirmPassword)
                        throw new Exception("两次输入的密码不一致");

                    user.password = Password;
                    return Json(new { success = true, message = "修改成功" });
                }

                
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

    }
}